package com.cl.blog.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cl.blog.common.Const;
import com.cl.blog.entity.BAdmin;
import com.cl.blog.entity.BMenuRole;
import com.cl.blog.entity.BRole;
import com.cl.blog.exception.WebException;
import com.cl.blog.mapper.BMenuRoleMapper;
import com.cl.blog.service.BAdminService;
import com.cl.blog.service.BMenuRoleService;
import com.cl.blog.service.BRoleService;
import com.cl.blog.mapper.BRoleMapper;
import com.cl.blog.util.JwtUtil;
import com.cl.blog.vo.RoleSearchVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwt;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author object
 * @description 针对表【b_role】的数据库操作Service实现
 * @createDate 2024-03-17 13:05:31
 */
@Service
public class BRoleServiceImpl extends ServiceImpl<BRoleMapper, BRole>
		implements BRoleService {

	@Autowired
	JwtUtil jwtUtil;
	@Autowired
	BAdminService bAdminService;
	@Autowired
	BMenuRoleMapper bMenuRoleMapper;

	@Override
	public List<BRole> selectOne(RoleSearchVo roleSearchVo) {
		if (roleSearchVo.getName().isEmpty()
				&& roleSearchVo.getStatus() == null
				&& roleSearchVo.getGrade().isEmpty()
				&& roleSearchVo.getCreateTime() == null) {
			throw new WebException("抱歉，搜索内容不能为空哦");
		}

		List<BRole> roleList = this.list(new QueryWrapper<BRole>()
				.eq(StringUtils.isNotBlank(roleSearchVo.getName()), "name", roleSearchVo.getName()) // 使用Apache Commons Lang的StringUtils判断字符串是否非空
				.eq(StringUtils.isNotBlank(roleSearchVo.getGrade()), "grade", roleSearchVo.getGrade())
				.eq(roleSearchVo.getStatus() != null, "status", roleSearchVo.getStatus())
				.gt(roleSearchVo.getCreateTime() != null, "create_time", roleSearchVo.getCreateTime())
		);

		return roleList;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateOrAddRole(BRole bRole, HttpServletRequest request) {

		String token = request.getHeader("token");
		Claims claim = jwtUtil.getClaimByToken(token, Const.GRADE_ADMIN);
		BAdmin bAdmin = bAdminService.getById(claim.getSubject());
		BRole bRole2 = this.getById(bAdmin.getRoleId());
		if (bRole2.getRole() != 0) {
			throw new WebException("抱歉，权限不足");
		}
		System.out.println(bRole2+"--"+bRole.getId());

		if (bRole.getId() != null) {
			// 角色存在 修改
			BRole bRole1 = this.getById(bRole.getId());
			Assert.notNull(bRole1, "出错了，稍后再试");
			bRole.setCreateTime(LocalDateTime.now());
			boolean save = this.update(new UpdateWrapper<BRole>()
					.eq("id",bRole.getId())
					.set("name",bRole.getName())
					.set("grade",bRole.getGrade())
					.set("role",bRole.getRole())
					.set("status",bRole.getStatus())
					.set("create_time",LocalDateTime.now())
			);
			// 删除所有权限
			boolean b = bMenuRoleMapper.removeNotIn(bRole.getId(), bRole.getMenuIds());
			// 新增权限
			for (Integer id : bRole.getMenuIds()) {
				BMenuRole bMenuRole = bMenuRoleMapper.selectOne(new QueryWrapper<BMenuRole>().eq("role_id",bRole.getId()).eq("menu_id",id));
				System.out.println(id+"--"+bRole.getMenuIds()+"--"+bMenuRole);
				if (bMenuRole == null) {
					BMenuRole bMenuRole1 = new BMenuRole();
					bMenuRole1.setMenuId(id);
					bMenuRole1.setRoleId(bRole.getId());
					bMenuRoleMapper.insert(bMenuRole1);
					System.out.println("执行了");
				}
			}
			System.out.println(bMenuRoleMapper.selectList(new QueryWrapper<BMenuRole>().eq("role_id", bRole.getId())));
			return true;
		} else {

			BRole grade = this.getOne(new QueryWrapper<BRole>().eq("grade", bRole.getGrade()));
			System.out.println(grade);
			Assert.isNull(grade, "抱歉，当前权限名称已存在");
			bRole.setCreateTime(LocalDateTime.now());
			boolean save = this.save(bRole);
			BRole grade1 = this.getOne(new QueryWrapper<BRole>().eq("grade", bRole.getGrade()));
			// 保存权限

			for (Integer menuId : bRole.getMenuIds()) {
				BMenuRole bMenuRole = new BMenuRole();
				bMenuRole.setRoleId(grade1.getId());
				bMenuRole.setMenuId(menuId);

				bMenuRoleMapper.insert(bMenuRole);
			}

			return true;
		}
	}
}




